/* eslint-disable */
Vue.mixin({
    methods: {
        reset(to) {
            if(WXEnvironment.platform === 'Web') {
                if (this.$router) {
                    this.$router.replace(to)
                }
            } else {
                if (this.$router) {
                    this.$router.replace(to)
                }
            }
        },
        getNavigator() {
            return weex.requireModule('navigator')
        },
        getModal () {
            return weex.requireModule('modal')
        },
        toBack() {
            if(WXEnvironment.platform === 'Web') {
                this.$router.back()
            }  else {
                this.getNavigator().pop({animated: "true"})
            }
        },
        getBaseUrl() {
            let bundleUrl = weex.config.bundleUrl;
            bundleUrl = String(bundleUrl);
            let nativeBase;
            let isAndroidAssets = bundleUrl.indexOf('file://assets/') >= 0;

            let isiOSAssets = bundleUrl.indexOf('file:///') >= 0 && bundleUrl.indexOf('WeexDemo.app') > 0;
            if (isAndroidAssets) {
                nativeBase = 'file://assets/dist/';
            }
            else if (isiOSAssets) {
                nativeBase = bundleUrl.substring(0, bundleUrl.lastIndexOf('/') + 1);
            } else {
                let host = 'localhost:8080';
                let matches = /\/\/([^\/]+?)\//.exec(bundleUrl);
                if (matches && matches.length >= 2) {
                    host = matches[1];
                }
                nativeBase = 'http://' + host + '/';
            }
            return nativeBase;
        },
        jumpInter(to) {
            if (this.$router) {
                this.$router.push(to)
            }
        },
        jumpWithParams(to, params) {
            if(WXEnvironment.platform === 'Web') {
                if (this.$router) {
                    this.$router.push({name: to, params: params})
                }
            } else {
                let path = this.getBaseUrl();
                let q = this.createQuery(params)
                this.getNavigator().push({
                    url: path + to + '.js' + q,
                    animated: "true"
                }, event => {
                    //modal.toast({ message: 'callback: ' + event })
                })
            }
        },
        // object 转 URL 参数
        createQuery(obj) {
            if(obj === null || obj === "" || obj.length === 0) {
                return ""
            }
            let url = '?';
            for (let key in obj) {
                if (obj[key] !== null) {
                    url += (key + '=' + encodeURIComponent(obj[key]) + '&');
                }
            }
            return url.substring(0, url.lastIndexOf('&'));
        },
        // 'xxx.js?name=aa' 转 {name: 'aa'}
        getQueryData(url) {
            // url = url.substring(url.indexOf('.js?') + 3);
            url = url.substring(url.indexOf('?'));
            let result = {};
            if (url.indexOf("?") != -1) {
                let str = url.substr(1);
                let strs = str.split("&");
                for (let i = 0; i < strs.length; i++) {
                    result[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
                }
            }
            return result;
        },
        getQuery() {
            if (WXEnvironment.platform === 'Web') {
                return this.$route.params;
            } else {
                return this.getQueryData(weex.config.bundleUrl);
            }
        },
        getUserInfo() {
            return this.$store.state.user.userInfo
        },
        // 真机alert
        alert (msg) {
            this.getModal().alert({
                message: msg
            })
        },
        // 真机toast
        toast (msg) {
            this.getModal().toast({
                message: msg
            })
        },
        validBase64 (str) {
            const reg = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i
            return reg.test(str)
        },
        formatDate (date, format = 'yyyy-MM-dd') {
            if (!date) return
            switch (typeof date) {
                case "string":
                    date = new Date(date.replace('T', ' ').replace(/-/, "/"))
                break
                case "number":
                    date = new Date(date)
                break
            }
            if (!(date instanceof Date)) return
            let dict = {
                "yyyy": date.getFullYear(),
                "M": date.getMonth() + 1,
                "d": date.getDate(),
                "H": date.getHours(),
                "m": date.getMinutes(),
                "s": date.getSeconds(),
                "MM": ("" + (date.getMonth() + 101)).substr(1),
                "dd": ("" + (date.getDate() + 100)).substr(1),
                "HH": ("" + (date.getHours() + 100)).substr(1),
                "mm": ("" + (date.getMinutes() + 100)).substr(1),
                "ss": ("" + (date.getSeconds() + 100)).substr(1)
            }
            return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function () {
                return dict[arguments[0]]
            })
        }
    }
})
